package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class DtsAdminGoodsController {
    @Autowired
    DtsGoodsService dtsGoodsService;
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsCategoryService categoryService;


    @RequestMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        Object result=dtsGoodsService.create(goodsAllinone);
        return result;
    }



    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsGoods dtsGoods){
        Object result=dtsGoodsService.delete(dtsGoods);
        return result;
    }


    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){

        return dtsGoodsService.update(goodsAllinone);
    }


    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){
        List<CatVo> catVos = categoryService.findCat();

        List<Map<String,Object>> brands=brandService.findBrand();

        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",catVos);
        data.put("brandList",brands);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/detail")
    public Object detail(Integer id){
        if (id==null) {
            return AdminResponseUtil.badArgument();
        }

        Map<String,Object> data = dtsGoodsService.findByDetail(id);
        return ResponseUtil.ok(data);
    }




    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "10")Integer limit,
                       String sort,String order,String goodsSn,String name){

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brands=new ArrayList<>();
        if(subject.hasRole("品牌制造商")){
             brands= brandService.findByAdminById(admin.getId());
        }

        IPage<DtsGoods> iPage=dtsGoodsService.findByPage(page,limit,sort,order,goodsSn,name,brands);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return ResponseUtil.ok(data);

    }
}
